package main

import (
	"github.com/muesli/cache2go"
	"testing"
	"time"
)

func main() {
	t := testing.T{}
	TestCacheExpire(&t)
}

func TestCacheExpire(t *testing.T) {
	k := "cache"
	v := "value"
	table := cache2go.Cache("testCache")

	table.Add(k+"_1", 100*time.Millisecond, v+"_1")
	table.Add(k+"_2", 125*time.Millisecond, v+"_2")

	time.Sleep(75 * time.Millisecond)

	// check key `1` is still alive
	_, err := table.Value(k + "_1")
	if err != nil {
		t.Error("Error retrieving value from cache:", err)
	}

	time.Sleep(75 * time.Millisecond)

	// check key `1` again, it should still be alive since we just accessed it
	_, err = table.Value(k + "_1")
	if err != nil {
		t.Error("Error retrieving value from cache:", err)
	}

	// check key `2`, it should have been removed by now
	_, err = table.Value(k + "_2")
	if err == nil {
		t.Error("Found key which should have been expired by now")
	}
}
